package com.emep.changzhi.analyse.http.basics;

import com.emep.changzhi.analyse.entity.basics.SysPollutantParameter;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.basics.PollutantParameterService;
import com.emep.changzhi.analyse.utils.DataValidator;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: LFY
 * @version:V1.0
 * @Description:
 * @Date: 2018/8/23 16:27
 */

@RestController
@RequestMapping("pollutant")
@Api(value="污染物信息controller",tags={"污染物信息"})
public class PollutantParameterController {

    @Autowired
    private PollutantParameterService service;

    /**
     * 新增污染物信息
     * @param payload
     * @return
     */
    @ApiOperation(value="新增污染物信息", notes="新增污染物信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pollutantName", value = "污染物名字", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "areaCode", value = "行政区划", required = true, dataType = "String",paramType="query")
    })
    @PostMapping(value = "add")
    public ResultBean<SysPollutantParameter> add(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "pollutantName","areaCode");
        if(result.getCode() == 1){
            return result.boolAdd(service.save(payload)) ;

        }
        return  result;
    }
    /**
     * 修改
     */
    @ApiOperation(value="修改污染物信息", notes="修改污染物信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pollutantName", value = "污染物名字", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "areaCode", value = "行政区划", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "isDelete", value = "是否删除", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "id", value = "污染物id", required = true, dataType = "String",paramType="query")

    })
    @PostMapping(value = "update")
    public ResultBean update(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id","pollutantName","areaCode","isDelete");
        if(result.getCode() == 1){
            return result.bool(service.update(payload)) ;

        }
        return  result;
    }


    /**
     * 根据id 删除污染物
     * @param payload
     */
    @ApiOperation(value="删除删除污染物信息", notes="根据id删除删除污染物信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "删除污染物id", required = true, dataType = "String",paramType="query"),
    })
    @GetMapping(value = "/delete")
    public ResultBean delete(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        String id=(String)payload.get("id");
        if (result.getCode() == 1){
            return result.bool(service.delete(id));
        }
        return result;
    }

    /**
     * 查询所有污染物信息
     * @return
     */
    @ApiOperation(value="查询所有污染物信息", notes="无参数查询污染物信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @GetMapping(value = "list")
    public ResultBean<List<SysPollutantParameter>> queryRegion(){
        return new ResultBean<List<SysPollutantParameter>>(service.findAll());
    }

    /**
     * 根据id查询污染物信息
     * @param payload
     * @return
     */
    @ApiOperation(value="根据id查询污染物信息", notes="根据id查询污染物信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "污染物id", required = true, dataType = "String",paramType="query"),
    })
    @GetMapping(value = "info")
    public ResultBean<SysPollutantParameter> findAreaById(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        String id=(String)payload.get("id");
        if (result.getCode() == 1) {
            return result.ok(service.findById(id));
        }
        return result;
    }

    /**
     * 根据areaCode查询污染物列表
     * @param payload
     * @return
     */
    @ApiOperation(value="areaCode查询监控站点列表", notes="areaCode查询监控站点列表")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areCode", value = "行政区划", required = true, dataType = "String",paramType="query")
    })
    @GetMapping(value = "list/areacode")
    public ResultBean<String> find(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "areaCode");
        String areaCode=(String)payload.get("areaCode");
        if (result.getCode() == 1) {
            return result.ok(service.findByAreaCode(areaCode));
        }
        return result;
    }


}
